package com.mzj.saas.mservice.community.controller;

import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.community.service.AppCommunityService;
import com.mzj.saas.mservice.community.vo.CommunityBuildUnitVO;
import com.mzj.saas.mservice.community.vo.CommunityBuildVO;
import com.mzj.saas.mservice.community.vo.CommunityStrVO;
import com.mzj.saas.mservice.community.vo.CommunityVO;
import com.mzj.saas.mservice.house.vo.HouseVO;
import com.mzj.saas.mservice.house.vo.OrgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 物业管理-小区管理-App
 */
@Controller
@RequestMapping("/appCommunity")
@Api(tags = "物业管理-小区管理")
public class AppCommunityController {
    @Autowired
    private AppCommunityService appCommunityService;

    /**
     * 查询小区列表
     * @param accessToken
     * @param pageNumber 分页参数
     * @param pageSize  分页大小
     * @param vo
     * @param idList  小区id
     *
     * 查询登录用户所属房产小区详细信息，如有条件查询将条件查询的条件添加进查询
     */
    @GetMapping("/findCommunityList")
    @ResponseBody
    @ApiOperation("查询小区列表")
    public ResultBean<ApiData<CommunityVO>> findCommunityList(@RequestHeader String accessToken,
                                                              @RequestParam(defaultValue = "0") Integer pageNumber,
                                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                                              CommunityVO vo,@RequestParam(required = false) List<String> idList) {
        return appCommunityService.findCommunityList(accessToken, pageNumber, pageSize, vo,idList);
    }

    /**
     * 新增小区
     * @param accessToken
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addCommunity", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean<Object> addCommunity(@RequestHeader String accessToken,
                                           @RequestBody CommunityVO vo) {
        return appCommunityService.saveCommunity(accessToken, vo);
    }

    /**
     * 修改小区
     */
    @RequestMapping(value = "/updateCommunity", method = RequestMethod.PUT)
    @ResponseBody
    public ResultBean<Object> updateCommunity(@RequestHeader String accessToken,
                                              @RequestBody CommunityVO vo) {
        return appCommunityService.saveCommunity(accessToken, vo);
    }

    /**
     * 删除小区
     */
    @RequestMapping(path = "/deleteCommunity/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultBean<Object> deleteCommunity(@RequestHeader String accessToken,
                                              @PathVariable String id) {
        return appCommunityService.deleteCommunity(accessToken, id);
    }

    /**
     * 楼宇列表
     * @param accessToken
     * @param pageNumber
     * @param pageSize
     * @param vo
     * @return
     * 查询本小区所有楼宇
     */
    @GetMapping("/findCommunityBuildList")
    @ResponseBody
    public ResultBean<ApiData<CommunityBuildVO>> findCommunityBuildList(@RequestHeader String accessToken,
                                                                        @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                                        CommunityBuildVO vo) {
        return appCommunityService.findCommunityBuildList(accessToken, pageNumber, pageSize, vo);
    }

    /**
     * 新增楼宇
     * @param accessToken
     * @param vo
     * @return
     *  添加楼宇判断当前是否存在楼宇存在即返回已存在，不存在即新增楼宇
     */
    @RequestMapping(value = "/addCommunityBuild", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean<Object> addCommunityBuild(@RequestHeader String accessToken,
                                                @RequestBody CommunityBuildVO vo) {
        return appCommunityService.saveCommunityBuild(accessToken, vo);
    }

    /**
     * 修改楼宇
     * @param accessToken
     * @param vo
     * @return
     * 修改楼宇判断当前是否存在楼宇存在即修改，不存在即新增楼宇
     */
    @RequestMapping(value = "/updateCommunityBuild", method = RequestMethod.PUT)
    @ResponseBody
    public ResultBean<Object> updateCommunityBuild(@RequestHeader String accessToken,
                                                   @RequestBody CommunityBuildVO vo) {
        return appCommunityService.saveCommunityBuild(accessToken, vo);
    }

    /**
     * 删除楼宇
     * @param accessToken
     * @param id
     * @return
     * 查询楼宇下是否有单元有单元无法删除
     */
    @RequestMapping(path = "/deleteCommunityBuild/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultBean<Object> deleteCommunityBuild(@RequestHeader String accessToken,
                                                   @PathVariable Long id) {
        return appCommunityService.deleteCommunityBuild(accessToken, id);
    }

    /**
     * 查询单元列表
     * @param accessToken
     * @param pageNumber
     * @param pageSize
     * @param vo
     * @return
     * 查询楼宇所有单元
     */
    @GetMapping("/findCommunityBuildUnitList")
    @ResponseBody
    public ResultBean<ApiData<CommunityBuildUnitVO>> findCommunityBuildUnitList(@RequestHeader String accessToken,
                                                                                @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                CommunityBuildUnitVO vo) {
        return appCommunityService.findCommunityBuildUnitList(accessToken, pageNumber, pageSize, vo);
    }

    /**
     * 新增单元
     * @param accessToken
     * @param voList  一个或多个单元
     * @return
     * 添加一个或多个单元、同一个楼宇下不能有重复单元
     */
    @RequestMapping(value = "/addCommunityBuildUnit", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean<Object> addCommunityBuildUnit(@RequestHeader String accessToken,
                                                    @RequestBody List<CommunityBuildUnitVO> voList) {
        return appCommunityService.addCommunityBuildUnit(accessToken, voList);
    }

    /**
     * 修改单元
     * @param accessToken
     * @param voList
     * @return
     * 修改单元判断当前是否存在楼宇存在即修改，不存在即新增单元
     */
    @RequestMapping(value = "/updateCommunityBuildUnit", method = RequestMethod.PUT)
    @ResponseBody
    public ResultBean<Object> updateCommunityBuildUnit(@RequestHeader String accessToken,
                                                       @RequestBody List<CommunityBuildUnitVO> voList) {
        return appCommunityService.updatCommunityBuildUnit(accessToken, voList);
    }

    /**
     * 删除单元
     * @param accessToken
     * @param id
     * @return
     * 查询楼宇下是否有房产有房产无法删除
     */
    @RequestMapping(path = "/deleteCommunityBuildUnit/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultBean<Object> deleteCommunityBuildUnit(@RequestHeader String accessToken,
                                                       @PathVariable Long id) {
        return appCommunityService.deleteCommunityBuildUnit(accessToken, id);
    }

    /**
     * 物业房产列表
     * @param accessToken
     * @param pageNumber
     * @param pageSize
     * @param vo
     * @return
     * 查询当前小区房产列表
     */
    @GetMapping("/findCommunityHouseList")
    @ResponseBody
    public ResultBean<ApiData<HouseVO>> findCommunityHouseList(@RequestHeader String accessToken,
                                                               @RequestParam(defaultValue = "0") Integer pageNumber,
                                                               @RequestParam(defaultValue = "10") Integer pageSize,
                                                               HouseVO vo) {
        return appCommunityService.findCommunityHouseList(accessToken, pageNumber, pageSize, vo);
    }


    /**
     * 新增物业房屋
     * @param accessToken
     * @param vo
     * @return
     * 添加物业房屋判断当前是否存在房屋存在，不存在即新增楼宇
     */
    @RequestMapping(value = "/addCommunityHouse", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("新增物业房屋")
    public ResultBean<Object> addCommunityHouse(@RequestHeader String accessToken,
                                                @RequestBody HouseVO vo) {
        return appCommunityService.saveCommunityHouse(accessToken, vo);
    }

    /**
     * 修改物业房屋
     * @param accessToken
     * @param vo
     * @return
     * 修改物业房屋判断当前是否存在楼宇存在即修改，不存在即新增楼宇
     */
    @RequestMapping(value = "/updateCommunityHouse", method = RequestMethod.PUT)
    @ResponseBody
    public ResultBean<Object> updateCommunityHouse(@RequestHeader String accessToken,
                                                   @RequestBody HouseVO vo) {
        return appCommunityService.saveCommunityHouse(accessToken, vo);
    }

    /**
     * 删除物业房产
     * @param accessToken
     * @param id
     * @return
     * 房产有账单的不可删除
     */
    @RequestMapping(path = "/deleteCommunityHouse/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultBean<Object> deleteCommunityHouse(@RequestHeader String accessToken,
                                                   @PathVariable String id) {
        return appCommunityService.deleteCommunityHouse(accessToken, id);
    }

    /**
     * 查询小区
     * @param accessToken
     * @return
     * 用户登录自动查询出名下管理的房屋 如是企业用户登录查询出企业用户名下房产 如是物业查询物业关联的小区
     */
    @GetMapping("/findCommunity")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> findCommunity(@RequestHeader String accessToken) {
        return appCommunityService.findCommunity(accessToken);
    }

    /**
     * 查询楼栋
     */
    @GetMapping("/findBuild")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> findBuild(@RequestParam(value = "communityId") String communityId) {
        return appCommunityService.findBuild(communityId);
    }

    /**
     * 查询单元
     */
    @GetMapping("/findUnit")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> findUnit(@RequestParam(value = "communityId") String communityId,
                                                          @RequestParam(value = "buildId") Long buildId) {
        return appCommunityService.findUnit(communityId, buildId);
    }

    /**
     * 查询房号[公租房+物业房产]
     */
    @GetMapping("/findRoomNumberByCommunityHouse")
    @ResponseBody
    public ResultBean<List<String>> findRoomNumberByCommunityHouse(@RequestParam(value = "communityId") String communityId,
                                                                   @RequestParam(value = "buildId") Long buildId,
                                                                   @RequestParam(value = "unitId") Long unitId) {

        return appCommunityService.findRoomNumberByCommunityHouse(communityId, buildId, unitId);
    }

    /**
     *小区总体详情
     * 根据不同的角色登录，查询不同的管辖小区信息 包含产权单位、物业
     */
    @GetMapping("/communityTotalDetails")
    @ResponseBody
    public ResultBean<Map<String, Object>> communityTotalDetails(@RequestHeader String accessToken) {

        return appCommunityService.communityTotalDetails(accessToken);
    }

    /**
     * 查询小区管理信息
     * @param accessToken
     * @param id 小区管理信息表id
     * @return
     */
    @GetMapping("/findCommunityInfo")
    @ResponseBody
    @ApiOperation("查询小区房产信息")
    public ResultBean<List<CommunityVO>> findCommunityInfo(@RequestHeader String accessToken, @RequestParam(required = true)
    @ApiParam(name = "id",value = "小区管理表id") String id) {
        return appCommunityService.findCommunityInfo(accessToken, id);
    }

    /**
     * 查询出对应区域下的所以产权单位/个人信息
     * @param accessToken
     * @param ownerType 产权归属类型：1产权单位  2个人
     * @return
     */
    @GetMapping("/AllProperty")
    @ResponseBody
    @ApiOperation("查询出对应区域下的所以产权单位/个人信息")
    public ResultBean<List<OrgVO>> AllProperty(@RequestHeader String accessToken,
                                               @RequestParam(value = "ownerType", required = true)
                                               @ApiParam(name = "ownerType",value = "产权归属类型：1产权单位  2个人") Integer ownerType) {
        return appCommunityService.AllProperty(accessToken, ownerType);
    }

    /**
     * 查询机构下对应区域的小区列表 20230224
     * @param accessToken
     */
    @GetMapping("/findAreaCommunityList")
    @ResponseBody
    @ApiOperation("查询机构下对应区域的小区列表")
    public ResultBean<List<CommunityStrVO>> findAreaCommunityList(@RequestHeader String accessToken) {
        return appCommunityService.findAreaCommunityList(accessToken);
    }

    /**20230306 xiaolong
     * 查询单个房产信息
     * @param accessToken
     * @param houseId 房产id
     * @return
     */
    @GetMapping("/findCommunityHouse")
    @ResponseBody
    @ApiOperation("查询单个房产信息")
    public ResultBean<List<HouseVO>> findCommunityHouse(@RequestHeader String accessToken,
                                                           @RequestParam(value = "houseId",required = true) String houseId) {
        return appCommunityService.findCommunityHouse(accessToken, houseId);
    }
}
